﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBO.Economy;
using Serveur.DataAccess;
using BusinessManagement;

namespace BusinessManagement.Economy
{
    public class PlayerEconomy
    {
        public static Resources GetPlayerResources(string token)
        {
            Session Sess = SessionContainer.SessionRepository[token];

            return _pda.GetPlayerResources(Sess.CityId);
        }

        public static int CountSlots(string token, string TroopType)
        {
            return _pda.GetPlayerSlotByType(SessionContainer.SessionRepository[token].CityId, TroopType).Number;
        }

        public static bool BuySlot(string token, string TroopType)
        {
            Resources CurrentRess = GetPlayerResources(token);
            Resources Price = ServerDataSingleton.Instance.ResourceRepository.Get<Resources>(TroopType);

            if (Price.Food <= CurrentRess.Food &&
                Price.Gold <= CurrentRess.Gold &&
                Price.Stone <= CurrentRess.Stone &&
                Price.Wood <= CurrentRess.Wood)
            {
                int cityID = SessionContainer.SessionRepository[token].CityId;
                _pda.AddSlot(cityID, TroopType);
                _pda.SubResource(cityID, Price);
                return true;
            }
            else
                return false;
        }

        public static bool SellSlot(string token, string TroopType)
        {
            int SlotsNumber = CountSlots(token, TroopType);
            Resources Price = ServerDataSingleton.Instance.ResourceRepository.Get<Resources>(TroopType);

            if (SlotsNumber > 0)
            {
                int cityID = SessionContainer.SessionRepository[token].CityId;
                _pda.DelSlot(cityID, TroopType);
                _pda.AddResource(cityID, Price);
                return true;
            }
            else
                return false;
        }

        public static string[] GetSquad(string token, int size)
        {
            string[] res = new string[16];

            foreach (DBO.Army.SquadSlot slot in _pda.GetSquadSlots(SessionContainer.SessionRepository[token].ArmyId, size))
            {
                res[slot.Column + 8 * slot.Line] = slot.TroopType;
            }

            return res;
        }

        public static void SetSquad(string token, string[] squad, int size)
        {
            List<DBO.Army.SquadSlot> DBOSquad = new List<DBO.Army.SquadSlot>();

            for (int i = 0; i < 16; ++i)
            {
                if (squad[i] == null || squad[i] == "")
                    continue;
                DBOSquad.Add(new DBO.Army.SquadSlot()
                {
                    Line = i / 8, Column = i % 8, TroopType = squad[i]
                });
            }
            _pda.SetSquadSlots(SessionContainer.SessionRepository[token].ArmyId, size, DBOSquad);
        }
        
        private static PlayerDataAccess _pda = new PlayerDataAccess();
    }
}
